﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using com.Norland.SPMS.Common .Data ;
using com.Norland.SPMS.Common.Model;
using com.Norland.SPMS.BusinessFacade;


namespace com.Norland.SPMS.WinUI.ClientLoader
{
	/// <summary>
	/// Form1 的摘要描述。
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.OpenFileDialog openFileDlg;

		private System.Windows.Forms.MenuItem miMDI;
		//private System.Windows.Forms.MenuItem[] miMdis;
		private MenuItem4form[] miMdis;

		private System.Windows.Forms.MenuItem miFormList;
		private System.Windows.Forms.MenuItem menuItem4;
		/// <summary>
		/// 設計工具所需的變數。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Windows Form 設計工具支援的必要項
			//
			InitializeComponent();

			LoginForm lf=new LoginForm();
			lf.ShowDialog();

		}

		/// <summary>
		/// 清除任何使用中的資源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form 設計工具產生的程式碼
		/// <summary>
		/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
		/// 這個方法的內容。
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.miMDI = new System.Windows.Forms.MenuItem();
			this.miFormList = new System.Windows.Forms.MenuItem();
			this.openFileDlg = new System.Windows.Forms.OpenFileDialog();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.miMDI,
																					  this.miFormList});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem4});
			this.menuItem1.Text = "File";
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 0;
			this.menuItem4.Text = "&Exit";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// miMDI
			// 
			this.miMDI.Index = 1;
			this.miMDI.Text = "MDI";
			// 
			// miFormList
			// 
			this.miFormList.Index = 2;
			this.miFormList.Text = "Form List";
			// 
			// openFileDlg
			// 
			this.openFileDlg.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDlg_FileOk);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(656, 445);
			this.IsMdiContainer = true;
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.Text = "Main Form";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.Closed += new System.EventHandler(this.MainForm_Closed);

		}
		#endregion

		/// <summary>
		/// 應用程式的主進入點。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
			
		}



		private void menuItem2_Click(object sender, System.EventArgs e)
		{ 
			openFileDlg.ShowDialog();          
            
		}


		private void openFileDlg_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}


		private void MainForm_Load(object sender, System.EventArgs e)
		{
			try
			{
				//取得数据库中的菜单项
				miMdis=WinFormSystem.getMenuItem4forms();
				foreach(MenuItem4form mi in miMdis)
				{
					mi.Click += new System.EventHandler(this.menuItem4lib_Click);
					miMDI.MenuItems.Add(mi);
					
				}
			}
				
			catch(Exception ex)
			{
				  
				MessageBox.Show(ex.Message);
			}
			miFormList.MdiList=true;
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void MainForm_Closed(object sender, System.EventArgs e)
		{
			//Application.Exit();
		}

		/// <summary>
		/// 专门用来响应加载窗体的事件,所有的窗体菜单单击事件都映射到此 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private  void menuItem4lib_Click(object sender, System.EventArgs e)
		{
			//MessageBox.Show(sender.ToString()); 
			//MessageBox.Show(((menuItem4lib)sender).formName); 
			//MessageBox.Show(((menuItem4lib)sender).libName); 
				
			WinFormSystem.loadMDIform(((MenuItem4form)sender).libName,((MenuItem4form)sender).formName,this);
		}
	}
}
